package www.wen.com.common.lib.di.module

import android.app.Application
import android.content.Context
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import www.wen.com.common.lib.data.db.AppDbHelper
import www.wen.com.common.lib.data.db.AppDbHelperImpl
import www.wen.com.common.lib.data.db.database.AppDatabase
import www.wen.com.common.lib.data.network.*
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.data.repository.AppDataManagerImpl
import javax.inject.Singleton

/**
 * @author WEN
 * @Description:
 * @date 2020/11/1 23:42
 */
@Module
@InstallIn(SingletonComponent::class)
class AppModule {

    @Provides
    @Singleton
    fun provideApplication(application: Application): Context {
        return application
    }

    @Provides
    fun provideSetCookieCache(): SetCookieCache {
        return SetCookieCache();
    }

    @Provides
    fun provideSharedPrefsCookiePersistor(context: Context): SharedPrefsCookiePersistor {
        return SharedPrefsCookiePersistor(context);
    }

    @Provides
    fun providePersistentCookieJar(
            setCookieCache: SetCookieCache,
            sharedPrefsCookiePersistor: SharedPrefsCookiePersistor
    ): PersistentCookieJar {
        return PersistentCookieJar(setCookieCache, sharedPrefsCookiePersistor);
    }

    @Provides
    fun provideConnectivityInterceptor(context: Application): ConnectivityInterceptor {
        return ConnectivityInterceptorImpl(context)
    }

    @Singleton
    @Provides
    fun provideApiService(
            connectivityInterceptor: ConnectivityInterceptor,
            cookieJar: PersistentCookieJar,
            application: Application
    ): ApiService {
        return ApiServiceClient.invoke(
                connectivityInterceptor,
                cookieJar,
                application
        )
    }

    @Singleton
    @Provides
    fun provideApiHelper(apiHelper: ApiHelperImpl): ApiHelper {
        return apiHelper
    }

    @Singleton
    @Provides
    fun provideAppDbHelper(apiHelperImpl: AppDbHelperImpl): AppDbHelper {
        return apiHelperImpl
    }

    @Singleton
    @Provides
    fun provideAppDataManager(appDataManagerImpl: AppDataManagerImpl): AppDataManager {
        return appDataManagerImpl
    }

    @Singleton
    @Provides
    fun provideAppDatabase(context: Context): AppDatabase {
        return AppDatabase.invoke(context)
    }

}